#include "areameasure.h"
#include "spatial3danalysis/analysis3dmeasurearea.h"
#include "globecontrol/globecontrol.h"
#include "measurelistener.h"

using namespace EarthView::World::Spatial3D::Controls;
using namespace EarthView::World::Spatial3D::Analysis;

AreaMeasure::AreaMeasure(QWidget *parent, Qt::WFlags flags)
	: QWidget(parent, flags)
{
	ui.setupUi(this);
	connect(ui.btnClear,SIGNAL(clicked()),SLOT(slotClear()));
	connect(ui.btnExit,SIGNAL(clicked()),SLOT(slotExit()));
	connect(ui.rbnhec,SIGNAL(clicked()),SLOT(slotAreaUnit()));
	connect(ui.rbnkilo,SIGNAL(clicked()),SLOT(slotAreaUnit()));
	connect(ui.rbnmi,SIGNAL(clicked()),SLOT(slotAreaUnit()));
	connect(ui.rbnmu,SIGNAL(clicked()),SLOT(slotAreaUnit()));
	//connect(ui.comboBox,SIGNAL(currentIndexChanged(const QString&)),SLOT(slotAreaUnit(const QString&)));
	mpAreaTool = NULL;
	mpGlobeControl = NULL;
	mpMeasureListener = NULL;
	this->setFixedSize(400,300);
}

void AreaMeasure::setText(const QString& clamTxt, const QString& projTxt)
{
	ui.leClampArea->setText(clamTxt);
	ui.leProjectArea->setText(projTxt);
}

void AreaMeasure::closeEvent(QCloseEvent *)
{
	slotClear();
	mpGlobeControl->setCurrentTool(NULL);
}

void AreaMeasure::setGlobeControl(CGlobeControl* ref_globeControl)
{
	mpGlobeControl = ref_globeControl;
	if(!mpMeasureListener)
	{
		mpMeasureListener = new CMeasureListener();
		mpMeasureListener->setWidget(this);
	}
	if(!mpAreaTool)
	{
		mpAreaTool = new CAnalysis3DMeasureArea(ref_globeControl);
		mpAreaTool->setUseHighestDem(false);
		mpAreaTool->setSearchInterval(1000);
		mpAreaTool->setAnalysis3DListener(mpMeasureListener);
	}
	ref_globeControl->setCurrentTool(mpAreaTool);
}

AreaMeasure::~AreaMeasure()
{
	if(mpGlobeControl)
		mpGlobeControl->setCurrentTool(NULL);
	if (mpAreaTool)
	{
		delete mpAreaTool;
		mpAreaTool = NULL;
	}
	delete mpMeasureListener;
}

void AreaMeasure::slotClear()
{
	if (mpGlobeControl->getCurrentTool()==mpAreaTool)
	{
		ui.leClampArea->setText("");
		ui.leProjectArea->setText("");
		mpMeasureListener->clearArea();
		mpGlobeControl->setCurrentTool(NULL);
		delete mpAreaTool;
		mpAreaTool= NULL;

		mpAreaTool = new CAnalysis3DMeasureArea(mpGlobeControl);
		mpAreaTool->setUseHighestDem(false);
		mpAreaTool->setSearchInterval(1000);
		mpAreaTool->setAnalysis3DListener(mpMeasureListener);
		mpGlobeControl->setCurrentTool(mpAreaTool);
	}
}

void AreaMeasure::slotExit()
{
	ui.leClampArea->setText("");
	ui.leProjectArea->setText("");
	mpMeasureListener->clearArea();
	mpGlobeControl->setCurrentTool(NULL);
	delete mpAreaTool;
	mpAreaTool= NULL;
	this->close();
}

void AreaMeasure::slotAreaUnit()
{
	if (mpMeasureListener)
	{
		if (ui.rbnhec->isChecked())
		{
			mpMeasureListener->setAreaUnit(EVString(ui.rbnhec->text().toStdString().c_str()));
			mpAreaTool->setAreaUnit(AU_HEKTARE);
		}
		else if (ui.rbnkilo->isChecked())
		{
			mpMeasureListener->setAreaUnit(EVString(ui.rbnkilo->text().toStdString().c_str()));
			mpAreaTool->setAreaUnit(AU_SQUAREMETER);
		}
		else if (ui.rbnmi->isChecked())
		{
			mpMeasureListener->setAreaUnit(EVString(ui.rbnmi->text().toStdString().c_str()));
			mpAreaTool->setAreaUnit(AU_SQUAREMETER);
		}
		else if (ui.rbnmu->isChecked())
		{
			mpMeasureListener->setAreaUnit(EVString(ui.rbnmu->text().toStdString().c_str()));
			mpAreaTool->setAreaUnit(AU_MU);
		}
	}
}
